{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.608973900Z",
     "start_time": "2023-09-10T13:45:51.018554800Z"
    }
   },
   "outputs": [],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "# Author:凌逆战 | Never\n",
    "# Date: 2023/8/14\n",
    "\"\"\"\n",
    "\n",
    "\"\"\"\n",
    "import numpy as np\n",
    "from scipy import signal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "class BiquadFilter():\n",
    "    def __init__(self, b, a):\n",
    "        self.b0, self.b1, self.b2 = b\n",
    "        _, self.a1, self.a2 = a\n",
    "        self.x1, self.x2, self.y1, self.y2 = 0, 0, 0, 0\n",
    "\n",
    "    def process(self, x):\n",
    "        # biquad公式：y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] - a1*y[n-1] - a2*y[n-2]\n",
    "        y = (self.b0 * x + self.b1 * self.x1 + self.b2 * self.x2 \n",
    "             - self.a1 * self.y1 - self.a2 * self.y2)\n",
    "        self.x2 = self.x1\n",
    "        self.x1 = x\n",
    "        self.y2 = self.y1\n",
    "        self.y1 = y\n",
    "        return y"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.613960100Z",
     "start_time": "2023-09-10T13:45:51.609971800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "# 设计高通滤波器系数\n",
    "fs = 16000  # 采样率\n",
    "fc = 70  # 截止频率（Hz）\n",
    "# 输入信号\n",
    "input_signal = [0.5, 0.8, 1.0, 0.7, -0.2, -0.6, -0.8, -0.3, -0.3, -0.3, -0.3]\n",
    "\n",
    "# 定义一个二阶高通滤波器(巴特沃斯)\n",
    "b, a = signal.butter(2, fc, btype='highpass', analog=False, output='ba', fs=fs)\n",
    "\n",
    "# 创建双二阶滤波器实例\n",
    "biquad_filter = BiquadFilter(b, a)\n",
    "output_signal_biquad = [biquad_filter.process(x) for x in input_signal]\n",
    "\n",
    "# 使用signal.butter进行前向滤波\n",
    "output_signal_butter = signal.lfilter(b, a, input_signal)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.627923Z",
     "start_time": "2023-09-10T13:45:51.617950Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Equal Outputs: True\n"
     ]
    }
   ],
   "source": [
    "# 判断两个输出结果是否相同\n",
    "print(\"Equal Outputs:\", np.allclose(output_signal_biquad, output_signal_butter, atol=1e-08))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.655848800Z",
     "start_time": "2023-09-10T13:45:51.654851400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.697736200Z",
     "start_time": "2023-09-10T13:45:51.654851400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.706712600Z",
     "start_time": "2023-09-10T13:45:51.677788900Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
